package com.gxa.eshops.sso.Controller;

import com.gxa.eshops.sso.entity.User;
import com.gxa.eshops.sso.service.UserService;
import com.gxa.eshps.cores.common.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.Pattern;
import java.util.Map;
/**
 * @program: IntelliJ IDEA
 * @description:
 * @author: Mr.Wang
 * @create: 2022-07-06 11:19
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "sso用户登录注册接口")
@Validated
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    @ApiOperation("一键登录注册接口")
    public Map<String ,Object> login(@RequestParam @Pattern(regexp = "^((13[0-9])|(15[^4,\\D])|(14[57])|(17[0])|(17[7])|(18[0,0-9]))\\d{8}",message = "手机号格式不正确")String phone
            ,@RequestParam @Pattern(regexp = "^[0-9]{4}",message = "验证码不能为空") String code) throws Exception {
        String token = userService.login(phone, code);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.set("token",token);
        return jsonResult.getMap();
    }

    @PostMapping("/checkWithToken")
    @ApiOperation("token获取用户信息")
    public Map<String ,Object> checkWithToken(String token) throws Exception {
        User user = userService.checkWithToken(token);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(user);
        return jsonResult.getMap();
    }
}
